﻿using System;
using System.Globalization;
using System.Threading;
using System.Web.UI;

namespace ADEVSO.Web.UI
{
    public class GlobalizationPage : Page
    {
        protected virtual void Page_Load(object sender, EventArgs e)
        {
            String actualURL = AppRelativeVirtualPath.Remove(0, 2).ToLower();
            if (Request.QueryString.Count > 0)
                actualURL += "?" + Request.QueryString.ToString().ToLower();
            ((URLHistory)Session["URLHistory"]).Add(actualURL);
            Session["CurrentPage"] = this;
            if (Master == null)
                ApplyCulture();
        }

        public void ApplyCulture()
        {
            ApplyCulture(false);            
        }

        public void ApplyCulture(bool InitializeHavingMaster)
        {
            InitializeCulture(InitializeHavingMaster);
            ApplyAdditionalCulture(InitializeHavingMaster);
        }

        public void ApplyCulture(String culture)
        {
            try
            {
                CurrentCulture = culture;
                InitializeCulture();
            }
            catch
            {
            }
        }

        protected virtual void ApplyAdditionalCulture()
        {
        }

        protected void ApplyAdditionalCulture(bool InitializeHavingMaster)
        {
            if (InitializeHavingMaster && CurrentCulture != Session["CurrentCulture"].ToString())
            {
                ApplyAdditionalCulture();
            }
        }

        protected override void InitializeCulture()
        {
            //InitializeCulture(false);
            try
            {
                if (Request.Cookies["CurrentCulture"] != null && CurrentCulture != Request.Cookies["CurrentCulture"].Value)
                    CurrentCulture = Request.Cookies["CurrentCulture"].Value;
                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(CurrentCulture);
                Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
            }
            catch
            {
                //throw;
            } 
        }

        protected void InitializeCulture(bool InitializeHavingMaster)
        {
            try
            {
                if (InitializeHavingMaster)
                {
                    InitializeCulture();
                }
                else if (CurrentCulture != Session["CurrentCulture"].ToString())
                    CurrentCulture = Session["CurrentCulture"].ToString();
            }
            catch
            {
                //throw;
            }
        }

        public String CurrentCulture
        {
            get
            {
                return (null != Session["CurrentCulture"]) ? Session["CurrentCulture"].ToString() : String.Empty;
            }
            set
            {
                if (Session["CurrentCulture"].ToString() != value)
                    Session["CurrentCulture"] = value;
            }
        }
    }
}
